package co.fitstart.admin.web.controller.courseware;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;

import co.fitstart.admin.service.DataService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.courseware.dietary.DietaryProgramTemplate;

/**
 * 
 * @author dan
 *
 */
@Controller
@RequestMapping("/dietary-program-templates")
@MenuSetting("menu-course")
public class DietaryProgramTemplateController {
    
    private static final Logger logger = LoggerFactory.getLogger(DietaryProgramTemplateController.class);
    
    @Autowired
    @Qualifier("dataService")
    private DataService<DietaryProgramTemplate> dietaryProgramTemplateService;
    
    /**
     * 
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-dietary-program-template")
    public ModelAndView list(Page page, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("courseware/dietary-program-template/dietary-program-template-list");
        
        List<DietaryProgramTemplate> dietaryProgramTemplates = dietaryProgramTemplateService.listByPage(DietaryProgramTemplate.class, page);
        modelAndView.addObject("dietaryProgramTemplates", dietaryProgramTemplates);
        
        modelAndView.addObject("page", page);
        
        modelAndView.addObject("refer", "list");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
        
        return modelAndView;
    }
    
    /**
     *view edit the movements
     * 
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView create() {
        
        ModelAndView modelAndView = new ModelAndView("courseware/dietary-program-template/dietary-program-template-create");
        
        return modelAndView;
    }
    
    
    /**
     * onCreate the movements
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public @ResponseBody String onCreate(DietaryProgramTemplate dietaryProgramTemplate) {
        
        Result result = new Result();
        
        try {
            
            dietaryProgramTemplateService.save(dietaryProgramTemplate);
            
            result.success().message("添加成功！");
            
        } catch(Exception e) {
            
            logger.error("#onCreate() - ", e);
            
            result.fail().message("添加失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     *view edit the movements
     * 
     * @return
     */
    @RequestMapping(value = "/{dietaryProgramTemplateId}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("dietaryProgramTemplateId") Long dietaryProgramTemplateId) {
        
        ModelAndView modelAndView = new ModelAndView("courseware/dietary-program-template/dietary-program-template-edit");
        
        DietaryProgramTemplate dietaryProgramTemplate = dietaryProgramTemplateService.load(DietaryProgramTemplate.class, dietaryProgramTemplateId);
        modelAndView.addObject("dietaryProgramTemplate", dietaryProgramTemplate);
        
        return modelAndView;
    }
    
    
    /**
     * onCreate the movements
     * 
     * @return
     */
    @RequestMapping(value = "/{dietaryProgramTemplateId}", method = RequestMethod.PATCH)
    public @ResponseBody String onEdit(@Preloading("dietaryProgramTemplateId") DietaryProgramTemplate dietaryProgramTemplate) {
        
        Result result = new Result();
        
        try {
            
            dietaryProgramTemplateService.update(dietaryProgramTemplate);
            
            result.success().message("修改成功！");
            
        } catch(Exception e) {
            
            logger.error("#onEdit() - ", e);
            
            result.fail().message("修改失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * View action-library delete panel
     * 
     * @param actionId
     * @return
     */
    @RequestMapping(value = "/{dietaryProgramTemplateId}/delete", method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable("dietaryProgramTemplateId") Long dietaryProgramTemplateId) {
        
        ModelAndView modelAndView = new ModelAndView("courseware/dietary-program-template/dietary-program-template-delete");
        
        modelAndView.addObject("dietaryProgramTemplateId", dietaryProgramTemplateId);
        
        return modelAndView;
    }
    
    /**
     * Do delete action-library 
     * 
     * @param actionId
     * @return
     */
    @RequestMapping(value = "/{dietaryProgramTemplateId}", method = RequestMethod.DELETE)
    public @ResponseBody String onDelete(@Preloading("dietaryProgramTemplateId") DietaryProgramTemplate dietaryProgramTemplate) {
         
        Result result = new Result();
        
        try {
            
            dietaryProgramTemplateService.update(dietaryProgramTemplate.disable());
            
            result.success().message("删除成功！");
            
        } catch(Exception e) {
            
            logger.error("#onDelete() - ", e);
            
            result.fail().message("删除失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
        
    }

}
